<template>
	<view>
		<u-cell-group>
			<u-cell-item @click='editPhone' title="更换手机号"></u-cell-item>
			<u-cell-item @click='setPassword' title="修改密码"></u-cell-item>
			<u-cell-item title="接收消息设置" @click='newsNavTo'></u-cell-item>
			<u-cell-item title="检查更新" @click='handleCheckForUpdates' :value="version.appVersion"></u-cell-item>
			<u-cell-item @click='clearFileSize' title="清除缓存" :value="fileSizeString"></u-cell-item>
			<u-cell-item title="账号注销" @click='accountCancellation'></u-cell-item>
		</u-cell-group>
		<view class="" style="padding: 0 24rpx;">
			<u-button @click="signOut" type="primary" style="margin-top: 25rpx;width: 100%;">退出登录</u-button>

		</view>
		<u-modal title="请填写" :show-cancel-button='true' v-model="showPaw" @confirm="confirm" ref="uModal"
			:async-close="true">
			<view class="paw_box">
				<u-field type='text' :password='true' :border-bottom='false' :error-message="errorMessage"
					v-model="passwordVal" label="密码:" placeholder="请填写密码">
				</u-field>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import cacheFunc from '@/utils/cache.js'
	import {
		logOut
	} from '@/utils/logOut.js'
	export default {
		data() {
			return {
				passwordVal: '',
				showPaw: false,
				errorMessage: '',
				fileSizeString: "0B",
				version: {}
			};
		},
		computed: {

		},
		mounted() {
			this.formatSize()
			this.checkForUpdates()
		},
		methods: {
			handleCheckForUpdates() {
				console.log(getApp().globalData.system)
				this.$u.api.checkForUpdates({
					type: getApp().globalData.system == 'android' ? 1 : 2
				}).then(res => {
					plus.runtime.getProperty(plus.runtime.appid, (appRes) => {
						const {
							appVersion,
							versionContent,
							url
						} = res

						console.log(appVersion, appRes, 'appResappRes')
						if (appVersion != appRes.version) {
							uni.showModal({
								title: '更新提醒',
								content: versionContent,
								success(type) {
									if (type.confirm) {
										plus.runtime.openURL(url)
									}
								}
							})
						} else {
							uni.showToast({
								title: '已是最新版本',
								icon: 'none'
							})
						}
					})

				})
			},
			checkForUpdates() {
				this.$u.api.checkForUpdates({
					type: getApp().globalData.system == 'android' ? 1 : 2
				}).then(res => {
					this.version = res
					console.log(res, 'res')
				})
			},
			newsNavTo() {
				uni.navigateTo({
					url: '/pages/setting/newsPages/newsPages'
				})
			},
			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			clearFileSize() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.formatSize();
					});
				}

			},
			accountCancellation() {
				// accountCancellation	
				uni.showModal({
					title: '提示',
					content: '确认注销吗?',
					success(res) {
						if (res.confirm) {
							that.$u.api.accountCancellation().then(res => {
								logOut('注销成功,请重新登录!')
							})
						}

					}
				})
			},
			confirm() {
				if (!this.passwordVal) {
					this.errorMessage = '请输入密码'
					this.$refs.uModal.clearLoading();
					return
				}
				this.$u.api.setPassword({
					passWord: this.passwordVal
				}).then(res => {
					uni.showToast({
						title: '密码设置成功!',
						icon: 'none'
					})
					this.$refs.uModal.clearLoading();
					this.errorMessage = ''
					this.passwordVal = ''
					this.showPaw = false;
				})
			},
			// setPassword
			setPassword() {
				uni.navigateTo({
					url: '/pages/editPassword/editPassword'
				})
				// this.showPaw = true
			},
			editPhone() {
				uni.navigateTo({
					url: '/pages/editPhone/editPhone'
				})
				// this.showPaw = true
			},
			signOut() {
				cacheFunc.removeCache('token')
				cacheFunc.removeCache('userInfo')
				uni.showToast({
					title: '退出成功',
					icon: 'none'
				})
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.paw_box {
		padding: 20rpx;
	}
</style>
